import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.SystemColor;

/**
 * This is responsible for starting the app/asking the user what type of player
 * they want.
 * 
 * @author GT Five-O
 * @version v1.0b
 * 
 */
public class PlayerConfig extends JFrame implements java.io.Serializable {

    private JPanel contentPane;
    private JTextField textField;
    private JComboBox races;
    private JComboBox colors;
    private static int numPlayers;
    private String difLevel;
    String[] info = new String[4];
    public static ArrayList<String[]> playerData = new ArrayList<String[]>();
    private static boolean selectionDone = false;
    private static int num = 0;
    private static String[] availableColors = GameConstants.PLAYER_COLORS;

    /**
     * Launch the application.
     */

    /**
     * creates the second setup screen before the player begins the game. It
     * asks the user for their player name, the type of race they want to select
     * (the player has options that include: "Humans", "Flapper", "Bonzoid",
     * "Ugaite", "Buzzite"), and the color of their player object (red, black,
     * green, yellow). Once the user enters these values, they tap the Done!
     * button to set their choices.
     * 
     * @param numPlayers
     */
    public PlayerConfig(int numPlayers, String level) {
        num++;
        difLevel = level;
        this.numPlayers = numPlayers;
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, GameConstants.DEFAULT_WIDTH, 594);
        setSize(GameConstants.DEFAULT_WIDTH, 594);
        contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.CENTER);
        panel.setLayout(null);

        JLabel lblEnterYourName = new JLabel("Enter Your Name:");
        lblEnterYourName.setFont(new Font(GameConstants.FONT, Font.PLAIN, 13));
        lblEnterYourName.setBounds(415, 196, 115, 16);
        panel.add(lblEnterYourName);

        textField = new JTextField();
        textField.setFont(new Font(GameConstants.FONT, Font.BOLD, 13));
        textField.setBounds(542, 190, 134, 28);
        panel.add(textField);
        textField.addKeyListener(new enterListener());
        textField.setColumns(10);

        JLabel lblRace = new JLabel("Race:");
        lblRace.setFont(new Font(GameConstants.FONT, Font.PLAIN, 13));
        lblRace.setBounds(483, 224, 38, 16);
        panel.add(lblRace);

        races = new JComboBox();
        races.setFont(new Font(GameConstants.FONT, Font.BOLD, 13));
        races.setModel(new DefaultComboBoxModel(new String[] { "Humans",
                "Flapper", "Bonzoid", "Ugaite", "Buzzite" }));
        races.setBounds(542, 220, 169, 27);
        races.addKeyListener(new enterListener());
        panel.add(races);

        JLabel lblPlayerColor = new JLabel("Player Color:");
        lblPlayerColor.setFont(new Font(GameConstants.FONT, Font.PLAIN, 13));
        lblPlayerColor.setBounds(443, 252, 87, 16);
        panel.add(lblPlayerColor);

        colors = new JComboBox();
        colors.setFont(new Font(GameConstants.FONT, Font.BOLD, 13));
        colors.setModel(new DefaultComboBoxModel(availableColors));
        colors.setBounds(542, 248, 102, 27);
        colors.addKeyListener(new enterListener());
        panel.add(colors);

        JButton btnDone = new JButton("Done!");
        btnDone.setFont(new Font(GameConstants.FONT, Font.BOLD, 13));
        btnDone.addActionListener(new singlePlayerInfo());
        btnDone.addKeyListener(new enterListener());
        btnDone.setBounds(542, 289, 117, 29);
        panel.add(btnDone);

        JLabel lblNumPlayers = new JLabel("Player " + num
                + ", please enter your info.");
        lblNumPlayers.setForeground(SystemColor.controlHighlight);
        lblNumPlayers.setFont(new Font(GameConstants.FONT, Font.BOLD, 13));
        lblNumPlayers.setHorizontalAlignment(SwingConstants.LEFT);
        lblNumPlayers.setBounds(415, 150, 500, 16);
        panel.add(lblNumPlayers);

        JLabel label = new JLabel("" + num);
        label.setHorizontalAlignment(SwingConstants.RIGHT);
        label.setForeground(SystemColor.windowBorder);
        label.setFont(new Font(GameConstants.FONT, Font.BOLD, 240));
        label.setBounds(828, 16, 161, 267);
        panel.add(label);

        JLabel lblNewLabel_2 = new JLabel("");
        lblNewLabel_2.setIcon(new ImageIcon("Images/start.png"));
        lblNewLabel_2.setBounds(0, 0, GameConstants.DEFAULT_WIDTH, 594);
        panel.add(lblNewLabel_2);

    }

    private class enterListener implements KeyListener {

        /**
         * when enter gets pressed, and if the key codes match, the action
         * performed by the user gets set to null
         */
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                new singlePlayerInfo().actionPerformed(null);
            }
        }

        @Override
        public void keyReleased(KeyEvent arg0) {
        }

        @Override
        public void keyTyped(KeyEvent arg0) {
        }

    }

    /**
     * gets player data based on the number of players.
     * 
     * @return
     */
    public ArrayList<String[]> getData() {
        if (numPlayers == 1) {
            return playerData;
        } else {
            return null;
        }
    }

    /**
     * returns the updated information based on the player's actions.
     * 
     * @param info
     * @return
     */
    public String[] infoUpdate(String[] info) {
        return info;
    }

    /**
     * boolean used within the setup screen.
     * 
     * @param flag
     */
    public void setDone(boolean flag) {
        selectionDone = flag;
    }

    /**
     * getter if the setup screen is updated.
     * 
     * @return
     */
    public boolean isDone() {
        return selectionDone;
    }

    private class singlePlayerInfo implements ActionListener {

        /**
         * action listener to get the user's inputs when the done button is
         * pressed. This method gets the user's name, race and color, and adds
         * these values to the to the player's object that is created. If only
         * one player is playing, the game instantly begins. If not, the game
         * asks all players to enter their selections. Once this is completed,
         * the game begins.
         */
        public void actionPerformed(ActionEvent e) {
            String text = textField.getText();

            /* Check if player entered a valid name */
            if (text.length() < 1 || text.equals("Enter a name.")) {
                textField.setText("Enter a name.");
                textField.setForeground(Color.RED);
            } else {
                String race = (String) (races.getSelectedItem());
                String color = (String) (colors.getSelectedItem());

                /* Remove selected color from options */
                int originalSize = availableColors.length;
                int newSize = originalSize - 1;
                String[] temp = new String[newSize];
                int itemOn = 0;
                for (int i = 0; i < originalSize; i++) {
                    if (!availableColors[i].equals(colors.getSelectedItem())) {
                        temp[itemOn] = availableColors[i];
                        itemOn++;
                    }
                }
                availableColors = temp;

                info[0] = text;
                info[1] = race;
                info[2] = color;
                info[3] = difLevel;
                playerData.add(info);
                if (numPlayers == 1) {
                    dispose();
                    setDone(true);
                } else {
                    dispose();
                    PlayerConfig nextPlayer = new PlayerConfig(numPlayers - 1, difLevel);
                    nextPlayer.setVisible(true);
                }
            }

        }
    }
}
